package tournamentData;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

public class GlobalScore {
	HashMap < Integer, ScorePerRound > scores;

	public GlobalScore() {
		this.scores = new HashMap < Integer, ScorePerRound >();
	}

	private ScorePerRound getScore(int round) {
		if (!this.scores.containsKey(round)) {
			this.scores.put(round, new ScorePerRound(0, 0, 0, 0));
		}
		return this.scores.get(round);
	}

	public int getPoints(int round) {
		return this.getScore(round).getPoints();
	}

	public int getXs(int round) {
		return this.getScore(round).getHits();
	}

	public int getTens(int round) {
		return this.getScore(round).getTens();
	}

	public int getNines(int round) {
		return this.getScore(round).getNines();
	}

	public void setPoints(int round, int points) {
		this.getScore(round).setPoints(points);
	}

	public void setHits(int round, int hits) {
		this.getScore(round).setHits(hits);
	}

	public void setTens(int round, int tens) {
		this.getScore(round).setTens(tens);
	}

	public void setNines(int round, int nines) {
		this.getScore(round).setNines(nines);
	}

	public void removeRound(int round) {
		this.scores.remove(round);
	}

	public ScorePerRound getScoreForRound(int round) {
		if (this.scores.containsKey(round)) {
			return this.scores.get(round);
		}
		return new ScorePerRound(0, 0, 0, 0);
	}

	public int getTotalPoints() {
		int points = 0;
		Iterator < Integer > iterator = this.scores.keySet().iterator();
		while (iterator.hasNext()) {
			points += this.getPoints(iterator.next());
		}
		return points;
	}

	public int getTotalXS() {
		int hits = 0;
		Iterator < Integer > iterator = this.scores.keySet().iterator();
		while (iterator.hasNext()) {
			hits += this.getXs(iterator.next());
		}
		return hits;
	}

	public int getTotalTens() {
		int tens = 0;
		Iterator < Integer > iterator = this.scores.keySet().iterator();
		while (iterator.hasNext()) {
			tens += this.getTens(iterator.next());
		}
		return tens;
	}

	public int getTotalNines() {
		int nines = 0;
		Iterator < Integer > iterator = this.scores.keySet().iterator();
		while (iterator.hasNext()) {
			nines += this.getNines(iterator.next());
		}
		return nines;
	}

	public LinkedList < Integer > getSavedRounds() {
		LinkedList < Integer > toReturn = new LinkedList < Integer >();
		Iterator < Integer > iterator = this.scores.keySet().iterator();
		while (iterator.hasNext()) {
			toReturn.add(iterator.next());
		}
		return toReturn;
	}

	/**
	 * @return [round,nines,tens,hits,points]
	 */
	public Integer [] getScoreAsArray(int round) {
		return new Integer [] { round, this.getPoints(round), this.getTens(round), this.getNines(round), this.getXs(round) };
	}

	@Override
	public String toString() {
		StringBuilder output = new StringBuilder();

		for (int i = 0; i < this.getSavedRounds().size(); i++) {
			output.append("Runde ");
			output.append(this.getSavedRounds().get(i));
			output.append(": ");
			output.append(this.getNines(this.getSavedRounds().get(i)));
			output.append(" | ");
			output.append(this.getTens(this.getSavedRounds().get(i)));
			output.append(" | ");
			output.append(this.getXs(this.getSavedRounds().get(i)));
			output.append(" | ");
			output.append(this.getPoints(this.getSavedRounds().get(i)));
			output.append("\n");
		}
		output.append("----------------------");

		return output.toString();
	}
}
